﻿

app.controller('RiskController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;


    $scope.ChangeFactory = function () {

    }

    $scope.GetRiskList = function (model, page) {
        $scope.RiskList = [];
        $http({
            method: 'POST',
            url: '../Risk/GetActiveRiskPageList',
            data: { 'page': page }
        }).success(function (ret) {
            common.success($scope, null, ret);
            if (ret.Code == 100) {
                $scope.RiskList = ret.Data.Data;
                console.log($scope.result);
            } else {
                alertify.alert(ret.Msg);
            }
        }).error(function (data, status, headers, config) {
            common.error($scope);
            alertify.alert("请检查网络");
        })
    }

    $scope.GetRiskLevelChart = function () {

        $http({
            method: 'POST',
            url: '../Risk/GetChartByRiskLevel',
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    var datalist = ret.Data;
                    var datasource = datalist;
                    var myChart = echarts.init(document.getElementById('chart1'));
                    var option = {
                        title: {
                            text:"固有风险等级占比"
                        },
                        tooltip: {
                            trigger: 'item'
                        },
                        legend: {
                        
                            left: 'center'
                        },
                        series: [
                            {
                                center: ['60%', '60%'],
                                name: '结果类型',
                                type: 'pie',
                                radius: '50%',
                                data: datasource,
                                emphasis: {
                                    itemStyle: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };
                    myChart.setOption(option, true);

                } else
                    alertify.alert(ret.Msg);

            })
            .error(function (data, status, headers, config) {
            });

    }

    $scope.GetAfterRiskLevelChart = function () {

        $http({
            method: 'POST',
            url: '../Risk/GetChartByAfterRiskLevel',
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    var datalist = ret.Data;
                    var datasource = datalist;
                    var myChart = echarts.init(document.getElementById('chart2'));
                    var option = {
                        title: {
                            text: "风险评定等级占比"
                        },
                        tooltip: {
                            trigger: 'item'
                        },
                        legend: {

                            left: 'center'
                        },
                        series: [
                            {
                                center: ['60%', '60%'],
                                name: '结果类型',
                                type: 'pie',
                                radius: '50%',
                                data: datasource,
                                emphasis: {
                                    itemStyle: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };
                    myChart.setOption(option, true);

                } else
                    alertify.alert(ret.Msg);

            })
            .error(function (data, status, headers, config) {
            });

    }

    $scope.GetChartByActive = function () {

        $http({
            method: 'POST',
            url: '../Risk/GetChartByActive',
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    var datalist = ret.Data;
                    var datasource = datalist;
                    var myChart = echarts.init(document.getElementById('chart3'));
                    var option = {
                        title: {
                            text: "风险评定等级占比"
                        },
                        tooltip: {
                            trigger: 'item'
                        },
                        legend: {
                            show:false,
                            left: 'right'
                        },
                        series: [
                            {
                                center: ['60%', '60%'],
                                name: '结果类型',
                                type: 'pie',
                                radius: '50%',
                                data: datasource,
                                emphasis: {
                                    itemStyle: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };
                    myChart.setOption(option, true);

                } else
                    alertify.alert(ret.Msg);

            })
            .error(function (data, status, headers, config) {
            });

    }

    $scope.BindDepartmentList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../SystemConfig/GetDepartmentList'
        })
            .success(function (ret) {
                $scope.PageLoading = false;
                common.success($scope, null, ret);
                if (ret.Code == 100) {

                    $.each(ret.Data, function (i, n) {
                        if (n.DepartmentName != "系统支持") {
                            if (defaultvalue != null) {
                                if (n.DepartmentName == defaultvalue)
                                    $("#" + control).append($compile("<option selected='selected' value=\"" + n.DepartmentName + "\">" + n.DepartmentName + "</option>")($scope));
                                else
                                    $("#" + control).append($compile("<option value=\"" + n.DepartmentName + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                            else {
                                $("#" + control).append($compile("<option value=\"" + n.DepartmentName + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }


    $scope.AddActiveRisk = function (model, searchmodel) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../Risk/AddActiveRisk',
            data: { 'model': model }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    alertify.alert("保存成功！");
                    $("#btn_close").click();
                    $scope.GetRiskList(searchmodel, 1);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })

    }

    $scope.UpdateActiveRisk = function (model, searchmodel,page) {
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../Risk/UpdateActiveRisk',
            data: { 'model': model }
        })
            .success(function (ret) {
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    alertify.alert("保存成功！");
                    $("#btn_close2").click();
                    $scope.GetRiskList(searchmodel, page);
                } else
                    alertify.alert(ret.Msg);
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                common.error($scope);
            })
    }

    $scope.DeleteActiveRisk = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除数据？", function (ev) {
                ev.preventDefault();
                $scope.DataLoading = true;
                $http({
                    method: 'POST',
                    url: '../Risk/DeleteActiveRisk',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert("操作成功！");
                            $scope.GetRiskList(searchmodel, page);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        $scope.DataLoading = false;
                        common.error($scope);
                    })
            }, function (ev) {
                ev.preventDefault();
            });
    }


    $scope.ShowDetail = function (model) {
        var tempmodel = JSON.parse(JSON.stringify(model))
        $scope.EditModel = tempmodel;
    }
});